#ifndef SERIALPORTMASTERTHREAD_H
#define SERIALPORTMASTERTHREAD_H

#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QtSerialPort/QSerialPort>

class SerialPortMasterThread : public QThread
{
    Q_OBJECT

public:
    SerialPortMasterThread(QObject *parent = 0);
    ~SerialPortMasterThread();

    void setSerialPortInstance(QSerialPort *portInstance);

    void transaction(QString portName,
                     qint32 baudRate,
                     QSerialPort::DataBits dataBits,
                     QSerialPort::Parity parity,
                     QSerialPort::StopBits stopBits,
                     QSerialPort::FlowControl flowControl,
                     int waitTimeout,
                     QString request);

    void stopTransaction();

    void setAutoSampling(bool autoSampling);

    void setSamplingPeriod(int millisecond);

    void run();

signals:
    void signalResponse(const QString &s);
    void signalError(const QString &s);
    void signalTimeout(const QString &s);

    void signalSerialPortConnected();

private:
    QSerialPort *m_portInstance;

    QString m_portName;
    qint32 m_baudRate;
    QSerialPort::DataBits m_dataBits;
    QSerialPort::Parity m_parity;
    QSerialPort::StopBits m_stopBits;
    QSerialPort::FlowControl m_flowControl;

    QString m_request;
    int m_waitTimeout;
    QMutex m_mutex;
    QWaitCondition m_waitCondition;
    bool m_quit;

    int m_autoSamplingPeriod;
    bool m_autoSampling;
};

#endif // SERIALPORTMASTERTHREAD_H
